#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""GIT callback logic class

Defines the logic for the callback page.
"""

__author__ = 'Xin Zhou(zhounew@google.com)'

from git.json import git_json
from git.util import git_context

class GitCallbackAction:
  FEDERATE_SUCCESS = 'federatedSuccess'
  FEDERATE_ERROR = 'federatedError'
  ERROR_TYPE_ACCOUNT_MISSMATCH = 'accountmismatch'
  ERROR_TYPE_INVALID_ASSERTION = 'invalidassertion'
  ERROR_TYPE_INVALID_ASSERTION_EMAIL = 'invalidassertionemail'
  
  def SaveAssertion(self, request, response):
    git_context.GitContext.GetSessionManager().SetAssertion(
        request.GetAssertion())
  
  def Notify_(self, status, params):
    html = ('<script type=text/javascript '
            'src="https://ajax.googleapis.com/jsapi"></script>'
            '<script type="text/javascript">'
            'google.load("identitytoolkit", "1.0", {packages: ["notify"]});'
            '</script>'
            '<script type="text/javascript">'
            'google.identitytoolkit.notify("%s", %s);'
            '</script>');
    html = html % (status, git_json.GitJson().dumps(params))
    return html
  
  def SendErrorMismatch(self, request, response):
    res = {}
    res['errorType'] = GitCallbackAction.ERROR_TYPE_ACCOUNT_MISSMATCH
    res['inputEmail'] = request.GetInputEmail()
    response.SetOutput(self.Notify_(GitCallbackAction.FEDERATE_ERROR, res))
  
  def SendErrorInvalidAssertion(self, request, response):
    res = {}
    res['errorType'] = GitCallbackAction.ERROR_TYPE_INVALID_ASSERTION
    response.SetOutput(self.Notify_(GitCallbackAction.FEDERATE_ERROR, res))
  
  def SendErrorInvalidAssertionEmail(self, request, response):
    res = {}
    res['errorType'] = GitCallbackAction.ERROR_TYPE_INVALID_ASSERTION_EMAIL
    response.SetOutput(self.Notify_(GitCallbackAction.FEDERATE_ERROR, res))
  
  def ShowHomePage(self, request, response):
    res = {}
    res['registered'] = True
    res['email'] = request.GetAccount().GetEmail()
    response.SetOutput(self.Notify_(GitCallbackAction.FEDERATE_SUCCESS,
                                    res))
  
  def ShowNewAccountPage(self, request, response):
    res = {}
    res['registered'] = False
    res['email'] = request.GetInputEmail()
    response.SetOutput(self.Notify_(GitCallbackAction.FEDERATE_SUCCESS,
                                    res))
  
  def Upgrade(self, request, response):
    verified_email = request.GetAssertion().GetVerifiedEmail()
    git_context.GitContext.GetAccountService().ToFederated(verified_email)
    # Update the account info in the session.
    account = git_context.GitContext.GetAccountService().GetAccountByEmail(
        verified_email)
    if account:
      git_context.GitContext.GetSessionManager().SetSessionAccount(
          account)
  
  def Login(self, request, response):
    git_context.GitContext.GetSessionManager().SetSessionAccount(
        request.GetAccount())